import extnames from '../dependencies/extnames.mjs';
import dirs from '../dependencies/dirs.mjs';
import path from 'path';
import fs from 'fs';
import readdirSync from '../utils/files/readdirSync.mjs';
import is_entry_file from '../dependencies/is_entry_file.mjs';

const _extnames_ = [...extnames.js, ...extnames.ts];

export default function (args, config) {
    if (!config.entry) { config.entry = {}; }

    const dir = path.join(dirs.run_project, 'src', 'js');

    if (!fs.existsSync(dir)) { return; }

    readdirSync(dir, false).forEach(filePath => {
        const extname = path.extname(filePath);
        if (!_extnames_.includes(extname)) { return; }

        const filename = path.basename(filePath);
        const filenameWithoutExtname = filename.substring(0, (filename.length - extname.length));
        if (!is_entry_file(filenameWithoutExtname, _extnames_, args.include, args.exclude)) { return; }

        if (extnames.ts.includes(extname)) {
            if (filename.endsWith('.d.ts')) { return; }
        }

        config.entry[filenameWithoutExtname + '.js'] = filePath;
    });
};
